package com.hj.enroll.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.hj.enroll.pojo.Admin;
import com.hj.enroll.pojo.User;
import com.hj.enroll.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping(produces = "application/json;charset=utf-8")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/toLogin")
    public String toLogin()
    {
        return "login";
    }

    //添加报名学生
    @PostMapping("/enter")
    public String insertUser(@RequestBody User user, HttpServletRequest request, HttpServletResponse response){
        //response.setContentType("application/json;charset=utf-8");

        //response.setHeader("Content-Type", "application/json");
        User u = userService.searchUser(user.getName());
        if (null != u){
            request.setAttribute("msg","该姓名已被报名注册，若同名请添加一个你喜欢的字符");
            return "h";
        }else {
            int i = userService.insertUser(user);
            if (i == 1){
                request.setAttribute("msg","报名成功！");

                System.out.println(user.getName());
                return user.getName();
            }else {
                request.setAttribute("msg","报名失败，字段不能为空！");

                return "h";
           }
        }

    }


}
